How to remove &nbsp from string in PHP?

Member

by wiley , in category: PHP , 2 years ago

How to remove &nbsp from string in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by brisa , 2 years ago

@wiley You can use html_entity_decode() function in PHP to remove &nbsp from any string, here is example:


1
2
3
4
5
6
<?php

$str = "Test &nbsp; string";

// Output: Test  string
echo html_entity_decode($str);


Member

by matteo , a year ago

@wiley 

To remove &nbsp; from a string in PHP, you can use the str_replace function:

1
$str = str_replace('&nbsp;', '', $str);


This will replace all occurrences of &nbsp; with an empty string in the $str variable.


Alternatively, you can use the html_entity_decode function to convert the &nbsp; entity to a regular space character, and then use trim to remove any leading or trailing white space from the string:

1
$str = trim(html_entity_decode($str));


This will remove any leading or trailing white space, including &nbsp;, from the string.